from humiture import *

class Youyun(Humiture):
    # 寄存器地址
    TMPERA_REG_ADDR = 0x00
    HUMI_REG_ADDR = 0x01
    TYPE_REG_ADDR = 0x64
    POINT_REG_ADDR = 0X65
    DEV_REG_ADDR = 0x66
    BAUD_REG_ADDR = 0X67
    MODE_REG_ADDR = 0X68
    PROTOCOL_REG_ADDR = 0x69
    # 功能码
    FUNC_WR = 0x06
    FUNC_RD = 0x03

    def __init__(self, id) -> None:
        super().__init__(id)

    def __no2baud(self, no):
        if no == 1:
            return 2400
        elif no == 2:
            return 4800
        elif no == 3:
            return 9600
        elif no == 4:
            return 19200
        elif no == 5:
            return 38400
        elif no == 6:
            return 115200
        
    def __baud2no(self, baud):
        if baud == 2400:
            return 1
        elif baud == 4800:
            return 2
        elif baud == 9600:
            return 3
        elif baud == 19200:
            return 4
        elif baud == 38400:
            return 5
        elif baud == 115200:
            return 6
        
    def dataParse(self, data):
        lcrc = []
        for i in range(len(data)):
            if data[i] == self.id:
                if data[i + 1] == self.FUNC_RD:
                    datalen = data[i + 2]
                    lcrc += data[i + datalen + 3: i +datalen + 5]
                elif data[i + 1] == self.FUNC_WR:
                    pass
                else:
                    return None
                if lcrc == crc.crc16(data[i:i + datalen + 3], True):
                    return data[i:i + datalen + 3]
                else:
                    return None
        return None
    
    def handler(self, data):
        pass
        # print("Received:%s\n" % bytes(data).hex())
        if len(data) < 9:
            print("Error")
            return
        
        tmp = self.dataParse(data)
        if not tmp:
            return -1
        data.clear()
        data += tmp
        if data[0] != self.id:
            return
        
        if data[1] == self.FUNC_RD:
            # pass
            # id = int.from_bytes(data[3:5], "big")
            # baud = self.__no2baud(int.from_bytes(data[3:5], "big"))
            temperature = int.from_bytes(data[3:5], "big") / 100
            
            humidity = int.from_bytes(data[5:7],"big") / 100
    
            # self.humiture.append(temperature)
            # self.humiture.append(humidity)
            # print(temperature, humidity)
            # self.threadLock.acquire()
            self.humitureflag = True
            # self.threadLock.release()
            return (1, temperature, humidity)
            pass
        elif data[1] == self.__WRITE_MAINTAIN_REG:
            pass
            if self.__ID_REG_ADDR == int.from_bytes(data[2:4], "big"):
                pass
                print("ID have been write %d\n" % int.from_bytes(data[4:6], "big"))
            elif self.__BAUD_REG_ADDR == int.from_bytes(data[2:4], "big"):
                pass
                print("Baud have been write %d\n" % self.__no2baud(int.from_bytes(data[4:6], "big")))
        elif data[1] == self.__AD_INPUT_REG:
            pass
            # self.humiture.clear()
            temperature = int.from_bytes(data[3:5], "big") / 10
            
            humidity = int.from_bytes(data[5:7],"big") / 10
    
            # self.humiture.append(temperature)
            # self.humiture.append(humidity)
            # print(temperature, humidity)
            # self.threadLock.acquire()
            self.humitureflag = True
            # self.threadLock.release()
            return (1, temperature, humidity)
        
    def humitureRead(self):
        return super().humitureRead(self.FUNC_RD, self.TMPERA_REG_ADDR)
    
    def idRead(self):
        return super().idRead(self.FUNC_RD, self.DEV_REG_ADDR)
    
    def writeId(self, id):
        return super().writeId(self.FUNC_WR, self.DEV_REG_ADDR, id)
    
    def baudRead(self):
        return super().baudRead(self.FUNC_RD, self.BAUD_REG_ADDR)
    
    def writeBaud(self, baud):
        return super().writeBaud(self.FUNC_WR, self.BAUD_REG_ADDR, baud)